package com.beauty.dressing.module.market.presenter;

import com.beauty.dressing.common.base.BasePresenter;
import com.beauty.dressing.common.bean.BaseObserver;
import com.beauty.dressing.common.bean.BaseParse;
import com.beauty.dressing.common.bean.LeaseItemDetail;
import com.beauty.dressing.common.bean.SaleItemDetail;
import com.beauty.dressing.common.bean.SalesOrderResp;
import com.beauty.dressing.module.market.presenter.contract.LeaseDetailContract;
import com.beauty.dressing.module.market.presenter.contract.SaleDetailContract;
import com.beauty.dressing.rxandroid.api.ApiManager;

import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import mlxy.utils.S;

/**
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/04/11
 */

public class SaleDetailPresenter extends BasePresenter implements SaleDetailContract.Presenter {

    private SaleDetailContract.ViewControl mViewControl;

    public SaleDetailPresenter(SaleDetailContract.ViewControl vcl) {
        this.mViewControl = vcl;
    }


    @Override
    public void getSalesDetail(long sales_id) {
        ApiManager.getInstence().getShopServie().saleDetail(sales_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SaleItemDetail,Object>() {

                    @Override
                    protected void onSuccees(String message, SaleItemDetail data,List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onSaleDetail(true, 200, message, data);
                    }

                    @Override
                    protected void onFailed(int code, String message, SaleItemDetail data,List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onSaleDetail(false, code, message, data);
                    }
                });
    }

    @Override
    public void salesAddCart(long sales_id, int number) {
        Map<String,String> map = httpParams.getHeaderMap();
        map.put("sales_id",String.valueOf(sales_id));
        map.put("sales_number",String.valueOf(number));
        ApiManager.getInstence().getOrderServie().saleAddShopcar(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object,Object>() {

                    @Override
                    protected void onSuccees(String message, Object data,List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onSaleAddCart(true, 200, message, data);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data,List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onSaleAddCart(false, code, message, data);
                    }
                });
    }

    @Override
    public void createSalesOrder(long leaseId) {
        Map<String,String> map = httpParams.getHeaderMap();
        map.put("leaseId",String.valueOf(leaseId));
        ApiManager.getInstence().getOrderServie().createSalesOrder(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SalesOrderResp,Object>() {

                    @Override
                    protected void onSuccees(String message, SalesOrderResp data,List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onCreateSaleOrder(true, 200, message, data);
                    }

                    @Override
                    protected void onFailed(int code, String message, SalesOrderResp data,List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onCreateSaleOrder(false, code, message, data);
                    }
                });
    }

}
